package com.geeguo.ebuilder.core.common.config;

import com.geeguo.ebuilder.core.common.utils.DateUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.beans.PropertyEditorSupport;
import java.util.Date;

@RestControllerAdvice
public class GlobalDataBinder {

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        /**
         * 处理请求参数到 Date 类型的转换
         */
        binder.registerCustomEditor(Date.class, new PropertyEditorSupport() {
            @Override
            public void setAsText(String text) {
            if (StringUtils.isBlank(text)) {
                setValue(null);
            } else {
                setValue(DateUtils.parseDate(text));
            }
            }
        });
    }
}
